# $Id: pystartup.py 44a91aebf028 2013/07/17 05:58:31 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
__version__  = "$Revision: 44a91aebf028 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2013-07-17 05:58:31Z $"

import readline
import rlcompleter
import os
import atexit
import sys

flag = True

_unm = os.uname()
if _unm[0] == "Darwin":
   readline.parse_and_bind ("bind ^I rl_complete")
else:
   readline.parse_and_bind('tab: complete')

readline.set_history_length(1000)
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')

if not os.path.exists(histfile):
   fh = None
   try:
      fh = open(histfile, "w")
   except IOError:
      flag = False
      pass
   else:
      if fh:
         fh.close()

      del fh

try:
   if os.path.exists(histfile):
      readline.read_history_file(histfile)
   else:
      flag = False
except IOError:
   pass

host = _unm[1].split(".")[0]
banner = """
---------------------------------------------------------------
 _  (`-')           (`-')      (`-').->           <-. (`-')_
 \-.(OO )     .->   ( OO).->   (OO )__      .->      \( OO) )
 _.'    \ ,--.'  ,-./    '._  ,--. ,'-'(`-')----. ,--./ ,--/
(_...--''(`-')'.'  /|'--...__)|  | |  |( OO).-.  '|   \ |  |
|  |_.' |(OO \    / `--.  .--'|  `-'  |( _) | |  ||  . '|  |)
|  .___.' |  /   /)    |  |   |  .-.  | \|  |)|  ||  |\    |
|  |      `-/   /`     |  |   |  | |  |  '  '-'  '|  | \   |
`--'        `--'       `--'   `--' `--'   `-----' `--'  `--'
---------------------------------------------------------------
"""
print(banner)
sys.ps1 = "(%s) [%s:%s]> " %(_unm[0], _unm[-1], sys.version.split(" ")[0])
sys.ps2 = "\001\033[91m....>> \033[0m\002"

if flag:
   atexit.register(readline.write_history_file, histfile)

del readline, rlcompleter, atexit, os, sys, _unm, flag, histfile, host
